import wx
import time

ID_EXIT = 200
ID_ABOUT = 201
Version = "0.1"
ReleaseDate = "2025-01-01"


class MainFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(500, 300))
        self.button_cancel = None
        self.button_ok = None
        self.status_bar = None
        self.timer = None

        # 状态栏的创建
        self.setup_status_bar()

        # 主菜单
        menubar = wx.MenuBar()
        # 子菜单：退出（Quit），将子菜单添加到File中
        menu_file = wx.Menu()
        menu_file.Append(ID_EXIT, "退出(Q)", "Quit application")
        menubar.Append(menu_file, "文件(F)")
        # 子菜单：关于（About），将子菜单添加到Help中
        menu_help = wx.Menu()
        menu_help.Append(ID_ABOUT, "关于(A)", "About application")
        menubar.Append(menu_help, "帮助(H)")
        self.SetMenuBar(menubar)

        # 菜单中子菜单，事件行为的绑定即实现
        self.Bind(wx.EVT_MENU, self.OnMenuExit, id=ID_EXIT)
        self.Bind(wx.EVT_MENU, self.OnMenuAbout, id=ID_ABOUT)
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

        # 显示按钮功能
        self.init_ui()

    def setup_status_bar(self):
        self.status_bar = self.CreateStatusBar(2)
        self.status_bar.SetStatusWidths([-1, -2])  # 状态栏[0]:状态栏[1]的比例 ==> 1:2
        self.status_bar.SetStatusText("Ready", 0)  # 设置状态栏[0]文本
        # Timer
        self.timer = wx.PyTimer(self.Notify)
        self.timer.Start(1000, wx.TIMER_CONTINUOUS)  # wx.TIMER_CONTINUOUS: 一直跑
        self.Notify()

    def init_ui(self):
        # 显示按钮功能
        # 增加面板 Panel
        self.panel = wx.Panel(self, -1)
        wx.StaticText(self.panel, label="Username: ", pos=(20, 20))
        wx.StaticText(self.panel, label="Password: ", pos=(20, 50))
        self._username = wx.TextCtrl(self.panel, pos=(85, 15))
        self._password = wx.TextCtrl(self.panel, pos=(85, 45), style=wx.TE_PASSWORD)
        self.submit_button = wx.Button(self.panel, label="提交", pos=(20, 80), size=(50, 30))
        self.panel.Bind(wx.EVT_BUTTON, self.OnClick, self.submit_button)

    def get_username(self):
        return self._username.GetValue()

    def get_password(self):
        return self._password.GetValue()
    def OnClick(self, event):
        if event.GetEventObject() == self.submit_button:
            dlg = LoginDialog(None, -1)
            dlg.ShowModal()
            dlg.Destroy()
        else:
            print("No button is clicked.")

    def Notify(self):
        time_local = time.localtime(time.time())
        strf_time_local = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
        self.status_bar.SetStatusText(strf_time_local, 1)

    def OnMenuExit(self, event):
        self.Close()

    def OnMenuAbout(self, event):
        dlg = AboutDialog(None, -1)
        dlg.ShowModal()
        dlg.Destroy()

    def OnCloseWindow(self, event):
        self.Destroy()


class AboutDialog(wx.Dialog):
    def __init__(self, parent, id):
        wx.Dialog.__init__(self, parent, id, "About me", size=(200, 200))
        self.InitUI()

    def InitUI(self):
        vbox = wx.BoxSizer(wx.VERTICAL)  # 垂直--自上而下
        vbox.Add(wx.StaticText(self, -1, "Hello, I'm wxPython"),
                 0, wx.ALIGN_CENTER_HORIZONTAL | wx.Top, border=20)
        vbox.Add(wx.StaticText(self, -1, "(C) 2025 HAHA工作室)"),
                 0, wx.ALIGN_CENTER_HORIZONTAL | wx.Top, border=10)
        vbox.Add(wx.StaticText(self, -1, f"Version {Version}, {ReleaseDate}"),
                 0, wx.ALIGN_CENTER_HORIZONTAL | wx.Top, border=10)
        vbox.Add(wx.StaticText(self, -1, "Author : HAHA"),
                 0, wx.ALIGN_CENTER_HORIZONTAL | wx.Top, border=10)
        vbox.Add(wx.Button(self, wx.ID_OK), 0, wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM, border=20)
        self.SetSizer(vbox)


class LoginDialog(wx.Dialog):
    def __init__(self, parent, id):
        super(LoginDialog, self).__init__(parent, id, "显示", size=(200, 200))
        self.app = wx.GetApp()  # --> r_128 App
        # Attributes
        self.panel = self.app.frame
        # Layout
        self._username_dlg = wx.StaticText(self, label="用户名: " + self.get_username(), pos=(20, 20))
        self._password_dlg = wx.StaticText(self, label="用户名: " + self.get_password(), pos=(20, 50))
        wx.Button(self, wx.ID_OK, pos=(20, 80))

    def get_username(self):
        return self.panel.get_username()
        # return self.app.frame.get_username()

    def get_password(self):
        return self.panel.get_password()
        # return self.app.frame.get_password()


class App(wx.App):
    def __init__(self):
        super(self.__class__, self).__init__()
        # wx.App.__init__(self)
        self.title = None
        self.version = None

    def OnInit(self):
        self.version = "lesson_6：静态文本、控制文本、对话框以及wx.GetApp()函数"
        self.title = "wxPython初级教程之" + self.version
        self.frame = MainFrame(None, -1, self.title)
        self.frame.Show(True)

        return True


if __name__ == "__main__":
    app = App()
    app.MainLoop()  # 启动主事件循环后，持续检查是否有事件需要处理，处理事件并更新界面，直到应用程序关闭。
